package org.wzl.todolisthelper.convert;

import org.mapstruct.AfterMapping;
import org.mapstruct.BeforeMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.wzl.todolisthelper.domain.NormalTask;
import org.wzl.todolisthelper.dto.AddNormalTaskDTO;
import org.wzl.todolisthelper.enumerate.DelFlagEnum;
import org.wzl.todolisthelper.enumerate.NormalTaskStatusEnum;
import org.wzl.todolisthelper.enumerate.TaskCardTagEnum;
import org.wzl.todolisthelper.enumerate.TaskDefinitionEnum;
import org.wzl.todolisthelper.enumerate.TaskPriorityEnum;
import org.wzl.todolisthelper.utils.IdCommonGen;
import org.wzl.todolisthelper.utils.TimeUtils;
import org.wzl.todolisthelper.utils.UserUtil;
import org.wzl.todolisthelper.vo.IndexTaskCardVO;
import org.wzl.todolisthelper.vo.NormalTaskDetailVO;
import org.wzl.todolisthelper.vo.TaskCardTagVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper(componentModel = "spring")
public interface NormalTaskConvert {

    NormalTask toDO(AddNormalTaskDTO addNormalTaskDTO);
    @BeforeMapping
    default void setDO(AddNormalTaskDTO addNormalTaskDTO, @MappingTarget NormalTask normalTask) {
        normalTask.setCreateUserId(UserUtil.getCurrentUserId());
        normalTask.setDelFlag(DelFlagEnum.NOT_DEL.getCode());
        normalTask.setId(IdCommonGen.generateId());
        normalTask.setTaskStatus(NormalTaskStatusEnum.DOING.getCode());
    }

    IndexTaskCardVO toVO(NormalTask allDO);
    @BeforeMapping
    default void setCardVO(@MappingTarget IndexTaskCardVO normalTaskCardVO, NormalTask allDO) {
        //设置标签
        List<TaskCardTagVO> tags;
        if (Objects.isNull(normalTaskCardVO.getTags())) {
            tags = new ArrayList<>();
            normalTaskCardVO.setTags(tags);
        } else {
            tags = normalTaskCardVO.getTags();
        }
        tags.add(TaskCardTagEnum.NORMAL_TASK.getTaskCardTagVO());

        if (allDO.getTaskPriority().equals(TaskPriorityEnum.LOW.getCommonActionSheet().getCode())) {
            tags.add(TaskCardTagEnum.LOW_PRIORITY.getTaskCardTagVO());
        } else if (allDO.getTaskPriority().equals(TaskPriorityEnum.MEDIUM.getCommonActionSheet().getCode())) {
            tags.add(TaskCardTagEnum.MEDIUM_PRIORITY.getTaskCardTagVO());
        } else if (allDO.getTaskPriority().equals(TaskPriorityEnum.HIGH.getCommonActionSheet().getCode())) {
            tags.add(TaskCardTagEnum.HIGH_PRIORITY.getTaskCardTagVO());
        }

        //设置时间区间
        normalTaskCardVO.setTaskTimeRange(TimeUtils.getTimeRange(allDO.getTaskStartDate(), allDO.getTaskEndDate()));

        normalTaskCardVO.setTaskType(TaskDefinitionEnum.NORMAL_TASK.getCode());

        normalTaskCardVO.setTimeLeft(TimeUtils.getTimeLeft(allDO.getTaskEndDate()));

    }

    NormalTaskDetailVO toDetailVO(NormalTask byId);
    @AfterMapping
    default void setDetailVO(@MappingTarget NormalTaskDetailVO normalTaskDetailVO, NormalTask byId) {
        if (byId.getTaskPriority().equals(TaskPriorityEnum.LOW.getCommonActionSheet().getCode())) {
            normalTaskDetailVO.setTaskPriority(TaskCardTagEnum.LOW_PRIORITY.getTaskCardTagVO().getText());
        } else if (byId.getTaskPriority().equals(TaskPriorityEnum.MEDIUM.getCommonActionSheet().getCode())) {
            normalTaskDetailVO.setTaskPriority(TaskCardTagEnum.MEDIUM_PRIORITY.getTaskCardTagVO().getText());
        } else if (byId.getTaskPriority().equals(TaskPriorityEnum.HIGH.getCommonActionSheet().getCode())) {
            normalTaskDetailVO.setTaskPriority(TaskCardTagEnum.HIGH_PRIORITY.getTaskCardTagVO().getText());
        }
    }
}
